package com.ssy.lingxi.purchase.controller.tender;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.StatusRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.TemplateRequest;
import com.ssy.lingxi.purchase.api.dto.response.tender.TemplateResponse;
import com.ssy.lingxi.purchase.entity.tender.Template;
import com.ssy.lingxi.purchase.service.tender.ITemplateService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: 招投标-评标模板管理
 * 评标模板管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/3/20
 */
@RestController
@RequestMapping("/purchase/template")
public class TemplateController extends BaseController {

    @Resource
    private ITemplateService templateService;

    /**
     * 查询评标模板列表
     * @param pageVO 分页实体
     * @param name 评标模板名称
     */
    @RequestMapping(value = "/getTemplateList", method = RequestMethod.GET)
    public Wrapper<PageData<TemplateResponse>> getTemplateList(PageVO pageVO, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "status",required = false) Boolean status) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Template> result = templateService.getTemplateList(sysUser, pageVO, name, status);
        List<TemplateResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<TemplateResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询评标模板信息
     * @param id 评标模板id
     */
    @RequestMapping(value = "/getTemplate", method = RequestMethod.GET)
    public Wrapper<TemplateResponse> getTemplate(@RequestParam("id") Long id) {
        Template template = templateService.getTemplate(id);
        if(template != null){
            return Wrapper.success(this.modelMapper.map(template, TemplateResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 新增/修改评标模板
     * @param templateRequest 评标模板实体
     */
    @RequestMapping(value = "/saveOrUpdateTemplate", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateTemplate(@RequestBody TemplateRequest templateRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(templateService.saveOrUpdateTemplate(sysUser, this.modelMapper.map(templateRequest, Template.class)));
    }

    /**
     * 修改评标模板状态
     * @param statusRequest 评标模板实体
     */
    @RequestMapping(value = "/updateTemplateStatus", method = RequestMethod.POST)
    public Wrapper<Boolean> updateTemplateStatus(@RequestBody StatusRequest statusRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(templateService.updateTemplateStatus(sysUser, statusRequest));
    }

    /**
     * 批量删除评标模板
     * @param simpleIdListRequest 参数
     */
    @RequestMapping(value = "/deleteBatchTemplate", method = RequestMethod.POST)
    public Wrapper<String> deleteBatchTemplate(@RequestBody SimpleIdListRequest simpleIdListRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(templateService.deleteBatchTemplate(sysUser, simpleIdListRequest.getIdList()));
    }

    /**
     * 查询该评标模板是否被使用
     * @param id 评标模板id
     */
    @RequestMapping(value = "/getTemplateIsUse", method = RequestMethod.GET)
    public Wrapper<Boolean> getTemplateIsUse(@RequestParam("id") Long id){
        return Wrapper.success(templateService.getTemplateIsUse(id));
    }
}
